package com.nlx.notes.module.service.impl.gold;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nlx.notes.module.bean.dto.gold.GoldOverviewEditDTO;
import com.nlx.notes.module.bean.dto.gold.GoldPurchaseAddDTO;
import com.nlx.notes.module.core.consts.GlobalConst;
import com.nlx.notes.module.core.enums.error.GoldCodeEnum;
import com.nlx.notes.module.entity.GoldOverview;
import com.nlx.notes.module.mapper.GoldOverviewMapper;
import com.nlx.notes.module.service.gold.IGoldOverviewService;
import com.nlx.notes.support.handler.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@Slf4j
@Service
public class GoldOverviewServiceImpl extends ServiceImpl<GoldOverviewMapper, GoldOverview> implements IGoldOverviewService {

    @Override
    public GoldOverview findNotCreateByUserId(Long userId) {
        if(null == userId){
            return null;
        }
        GoldOverview goldOverview = this.getBaseMapper().selectOne(Wrappers.lambdaQuery(GoldOverview.class)
                .eq(GoldOverview::getUserId, userId).eq(GoldOverview::getDelFlag, GlobalConst.DEL_FLAG_NORMAL), false);
        if(null != goldOverview){
            return goldOverview;
        }
        //当前数据不存在，创建一条数据
        this.createUserGoldOverview(userId);
        return findNotCreateByUserId(userId);
    }

    @Override
    public void addGold(GoldPurchaseAddDTO goldPurchaseAddDTO) {
        GoldOverview goldOverview = this.findNotCreateByUserId(goldPurchaseAddDTO.getUserId());
        if(null == goldOverview){
            log.error("current user not have gold overview, userId -> {}",goldPurchaseAddDTO.getUserId());
            throw new BusinessException(GoldCodeEnum.SAVE_GOLD_ERROR);
        }
        BigDecimal weight = goldOverview.getWeight().add(new BigDecimal(goldPurchaseAddDTO.getWeight()));
        goldOverview.setWeight(weight);
        BigDecimal totalPrice = goldOverview.getTotalPrice().add(new BigDecimal(goldPurchaseAddDTO.getTotalPrice()));
        goldOverview.setTotalPrice(totalPrice);
        //计算平均价格
        goldOverview.setUnitPrice(totalPrice.divide(weight,2, RoundingMode.HALF_UP));
        goldOverview.setUpdatedTime(LocalDateTime.now());
        this.updateById(goldOverview);
    }

    @Override
    public boolean editGold(GoldOverviewEditDTO goldOverviewEditDTO) {
        GoldOverview goldOverview = findNotCreateByUserId(goldOverviewEditDTO.getUserId());
        if(null == goldOverview){
            log.error("user ->{} not exists gold overview", goldOverviewEditDTO.getUserId());
            return false;
        }
        goldOverview.setWeight(goldOverviewEditDTO.getWeight());
        goldOverview.setTotalPrice(goldOverviewEditDTO.getTotalPrice());
        goldOverview.setUnitPrice(goldOverviewEditDTO.getUnitPrice());
        goldOverview.setUpdatedTime(LocalDateTime.now());
        return this.updateById(goldOverview);
    }

    private void createUserGoldOverview(Long userId) {
        GoldOverview goldOverview = new GoldOverview();
        goldOverview.setWeight(new BigDecimal("0"));
        goldOverview.setTotalPrice(new BigDecimal("0"));
        goldOverview.setUnitPrice(new BigDecimal("0"));
        goldOverview.setTotalProfitLoss(new BigDecimal("0"));
        goldOverview.setUserId(userId);
        goldOverview.setRemarks(null);
        goldOverview.setCreatedTime(LocalDateTime.now());
        goldOverview.setUpdatedTime(LocalDateTime.now());
        goldOverview.setDelFlag(GlobalConst.DEL_FLAG_NORMAL);
        this.save(goldOverview);
    }
}
